{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7370ff28",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "\n",
    "\n",
    "\n",
    "# 本周知识\n",
    "> 1.函数的返回值\n",
    "\n",
    "> 2.函数的输入参数类型\n",
    "\n",
    "> 3.函数的返回参数类型\n",
    "\n",
    "> 4.模块的概念\n",
    "\n",
    "> 5.如何创建一个模块，并且调用自己定义的模块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01a49028",
   "metadata": {},
   "source": [
    "## 函数的返回值\n",
    "> 1.返回值关键字：return\n",
    "\n",
    ">> A.函数一定有返回值（无论有什么编程语言）\n",
    "\n",
    ">> B.在其他的编程语言中（如C语言，java等），返回值一定要写，不写会报错；（更高级的语言），返回值如果不写，给定默认值Non\n",
    "\n",
    ">> C.返回值一般为函数体运行的结果（result）（函数返回值实践1）；也可以是自定义的函数返回值（实践2）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d0282a5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#函数返回值实践1\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+last_name\n",
    "#print(full_name)\n",
    "    return full_name   #返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7b132131",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'li hua'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_formatted_name(\"li\",\"hua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0e9ca769",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "li hua\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"li\",\"hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ad8df6f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回值实践2\n",
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"lihua\" and password ==123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e1688d5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"lihua\",123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8895962f",
   "metadata": {},
   "source": [
    "## 函数的输入参数类型\n",
    "> 1.函数可以输入复杂的参数：\n",
    "    \n",
    "   >> a.函数参数为列表\n",
    "    \n",
    "   >> b.函数参数为字典\n",
    "   >> c.....\n",
    "\n",
    "> 2.函数可以输入不确定个数的参数：\n",
    "    \n",
    "   >> a.*args\n",
    "   >> b.**kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "df51781f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_A(names):\n",
    "    \"\"\"测试函数参数输入为列表\"\"\"\n",
    "    return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "63c774a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['lihua', 'Alex', 'Ziqi']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_A([\"lihua\",\"Alex\",\"Ziqi\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bba723b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_B(person):\n",
    "    \"\"\"测试函数参数输入为字典\"\"\"\n",
    "    return person\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f1898046",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 201023, 'age': 20, 'job': 'student'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_B({\"stud_id\":201023,\"age\":20,\"job\":\"student\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55597cdd",
   "metadata": {},
   "source": [
    "## 函数的输出（返回值）参数类型\n",
    "> 1.在编程语言中，函数的返回值只能是一个值\n",
    "\n",
    "> 2.返回值可以是：字符串，数值，列表，字典等数据（单一值）\n",
    "\n",
    "> 3.python可以输出多个返回值，结果会用一个元组进行封装并返回，（本质不违背函数的定义）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "0f9f5270",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\" 测试返回值return为字符串\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f4205dce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "4842f50f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return为数值\"\"\"\n",
    "    return 12345\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5877902d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "78ca566f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return为列表\"\"\"\n",
    "    return ['Lihua', 'Alex', 'Ziqi']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0ece00b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Ziqi']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "006ec42f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return为字典\"\"\"\n",
    "    return {'stud_id': 210001, 'age': 18, 'Job': 'student'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b65f8f1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "772f6535",
   "metadata": {},
   "outputs": [],
   "source": [
    " # ******** return 后面写多个值，将会返回一个元组（一个值，不违背函数在编程思维中的定义）\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return 为 多个值的情况\"\"\"\n",
    "    return 18,\"Lihua\",['Lihua', 'Alex', 'Ziqi'],{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "49827185",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18,\n",
       " 'Lihua',\n",
       " ['Lihua', 'Alex', 'Ziqi'],\n",
       " {'stud_id': 210001, 'age': 18, 'Job': 'student'})"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f3173112",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test6_return():\n",
    "    \"\"\"测试返回值return 为 多个值的情况\"\"\"\n",
    "    return 88,89,90,90"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f870d42e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(88, 89, 90, 90)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test6_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2aeaff6",
   "metadata": {},
   "source": [
    "## 模块\n",
    ">1. 使用模块可以隐藏函数的源代码\n",
    "\n",
    "> 2.使用的人不需要会底层代码（只需要会使用模块即可）\n",
    "\n",
    "> 模块的使用的方法：\n",
    "> * import 模块名\n",
    "使用： 模块名.函数名()\n",
    "> * from 模块名 import 函数名\n",
    "使用： 函数名()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7fadd66a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c4bb76c",
   "metadata": {},
   "source": [
    "time.localtime()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a1a7cb69",
   "metadata": {},
   "outputs": [],
   "source": [
    "from time import localtime\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "902f2eef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=30, tm_hour=10, tm_min=12, tm_sec=51, tm_wday=3, tm_yday=364, tm_isdst=0)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "localtime()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b2d54124",
   "metadata": {},
   "outputs": [],
   "source": [
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "82cab6c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#函数的调用\n",
    "study.search4vowels(\"hello zhichao\")      # 成功了，太棒了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1024042d",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'study' has no attribute 'news_链接'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_7348/731484358.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhelp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstudy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnews_链接\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: module 'study' has no attribute 'news_链接'"
     ]
    }
   ],
   "source": [
    "help(study.news_链接)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "121b90c6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
